// pages/user/index.js
Page({
  data: {
   userInfo:{},
   loginStatus:false,
  },

  onShow(){
    this.getUserinfo();
  },
  getUserinfo(){
    const userInfo = wx.getStorageSync('userinfo');
    if(!userInfo){
      this.setData({
        loginStatus:true
      });
    }else{
      this.setData({
        loginStatus:false
      });
    }
    this.setData({
      userInfo
    });

    // 小程序获取token 不需要登录也行
    wx.request({
      url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx05c6a45f75f2a68f&secret=8b51bc912faf4e4961c2367e1dba2fbc',
      success (res) {
        console.log('res.data',res.data.access_token);
        const token = res.data.access_token;
        wx.setStorageSync('token', token)
        console.log('token',token);
      }
    })

  },
  bindgetuserinfo(e){
    wx.setStorageSync('userinfo', e.detail.userInfo);
    this.getUserinfo();

  },
  unLogin(){
    var vm = this;
    wx.showModal({
      title: '提示',
      content: '确定退出登录吗？',
      success (res) {
        if (res.confirm) {
          wx.clearStorage({
            success: (res) => {
             setTimeout(()=>{
              wx.showToast({
                title: '退出登录成功',
              })
             },1000);
              vm.getUserinfo();
            },
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  }

})